hirom   ; don't change this
; header  ; comment out if your rom has no header

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; A large part of the commenting on this code is derived directly
; from the commented code banks available at:
; http://slickproductions.org/docs.php?id=FF6
; and would not have been possible without that commenting.
; 
; Additionally, the thread located at: 
; http://www.ff6hacking.com/forums/showthread.php?tid=729
; was instrumental in the production of this hack
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; SKILLS LEARNED AT LEVEL UP
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; This points to the function governing learning abilities at 
; level up, originally at C2/61B6 and now at C2/A736.
org $C260BC
JSR $A736   

; Determine if the character learns natural magic at level up.
org $C2A736
LDX #$0000       ; Relm's spell list is the first two bytes
                 ; of the natural magic table now located at
                 ; EC/E3A0. In vanilla, this starts at EC/E3C0
                 ; with Terra's table; Relm's occupies two bytes
                 ; of free space preceding.
CMP #$08         ; 
BEQ LvlUpMagic   ; Branches to teach natural magic.
LDX #$0020       ; Terra's spell list is second in the table.
CMP #$00         ; 
BEQ LvlUpMagic   ; 
LDX #$0040       ; Celes' spell list is third.
CMP #$06         ; 
BEQ LvlUpMagic   ; 
LDX #$0000       ; Cyan's SwdTech list is the first eight bytes
                 ; of the SedTech/Blitz table at E6/F490
CMP #$02         ; 
BNE LvlUpSabin   ; If not Cyan, branch to check for Sabin.
                 ; Else, it is Cyan and the code can continue
                 ; to check his level.

;;;;;;;;;;;;;;;;;;
; Cyan @ level up
;;;;;;;;;;;;;;;;;;
JSR LvlAbility   ; Check for SwdTechs at this level.
BEQ ExitLearning ; If none, exit.
TSB $1CF7        ; Enable the new SwdTech.
BNE ExitLearning ; If already enabled, exit.
                 ; This should only occur after Cyan's dream,
                 ; which teaches him SwdTechs through an event.
LDA #$40
TSB $F0
BNE ExitLearning
LDA #$42
JMP $5FD4

;;;;;;;;;;;;;;;;;;
; Sabin @ level up
;;;;;;;;;;;;;;;;;;
LvlUpSabin:
LDX #$0008       ; Blitzes are in the second byte of the table.
CMP #$05         ; Check for Sabin.
BNE ExitLearning ; Exit if not Sabin.
JSR LvlAbility   ; Check for Blitzes at this level.
BEQ ExitLearning ; If none, exist.
TSB $1D28        ; Enable the new Blitz.
BNE ExitLearning ; If already enable, exit.
                 ; This should only occur with Bum Rush, after
                 ; meeting Duncan in the WoR.
LDA #$80
TSB $F0
BNE ExitLearning
LDA #$33
JMP $5FD4

;;;;;;;;;;;;;;;;;;
; Magic @ level up
;;;;;;;;;;;;;;;;;;
LvlUpMagic:
PHY 
XBA 
LDY #$0010       ; The spell list is composed of 16 2-byte
                 ; blocks for a total of 32 bytes.
LvlCheckMag:
CMP $ECE3A1,X    ; Check against level at which spell is learned
BNE IncSpell     ; If no spell at this level, branch and 
                 ; increment to next spell in table.
PHA 
PHY 
TDC              ; [Clear 16-bit A]
LDA $ECE3A0,X    ; Get the spell ID from the table.
TAY 
LDA ($F4),Y      ; [(?? check entry in spell list ??)]
CMP #$FF       
BEQ PullYA     
LDA #$80
STA ($F4),Y
PullYA:
PLY 
PLA 
IncSpell:        ; Increment to next 2-byte block.
INX 
INX      
DEY              ; Decrement Y
BNE LvlCheckMag  ; If we haven't checked all 32 bytes,
                 ; branch back and continue.
PLY 
RTS 

;;;;;;;;;;;;;;;;;;
; Abilities/lvl up
;;;;;;;;;;;;;;;;;;
LvlAbility:
LDA #$01
STA $EE          ; Start by marking Tech/Blitz as #0
XBA              ; [Get current level from top of A?]
LvlCheckAbi:     ;
CMP $E6F490,X    ; Check level against Blitz/Tech table.
BEQ AbiBitfield  ; If equal, branch.
INX              ; Else, increment to check next index.
ASL $EE          ; Check next Tech against level.
BCC LvlCheckAbi  ; If carry is set, we've checked all 8 bits and 
                 ; no ability is learned. $EE will be #0. 
AbiBitfield:
LDA $EE          ; Get the SwdTech/Blitz bitfield. The bit that
                 ; is set = the ability to learn at this level. 

ExitLearning:
RTS 




;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; DEFAULT SKILLS AT RECRUITMENT
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


; This section governs natural abilities learned at
; recruitment

;;;;;;;;;;;;;;;;;;
; DEFAULT DANCES
;;;;;;;;;;;;;;;;;;
; This sets default dances learned at the same time as default
; lores and rages. Rather than pulling the information from 
; elsewhere in the rom, the bit is hardcoded in with an LDA
; to keep the code in-line.
;
; Originally I was teaching dances at recruitment the same way
; that Cyan and Sabin learn their initial SwdTechs and Blitzes.
; However, because they learn their skills through level up, 
; they require more complex code to verify which skills they
; should already have when you meet them. This new code teaches
; Wind Song by default in the same way that Strago and Gau learn
; their default Lores and Rages, respectively.

org $C0BE2F  ; Originally this is used to set font to white.
             ; As this is also in C3, it's unnecessary here, and
             ; so we can use these addresses for teaching Dances.
             ; It follows immediately after the code teaching
             ; the default Lores.
LDA #$01     ; Load bits for default Dances.
             ; #$01 is Wind Song; set other bits to teach other
             ; dances by default.
STA $1D4C    ; Stores bits in location of learned Dances in RAM
NOP          ; The original code here is six bytes long.
             ; As the new code is only five, this blocks out the 
             ; remaining byte preventing any further snarls.

;;;;;;;;;;;;;;;;;;
; NATURAL SKILLS
; AT RECRUITMENT
;;;;;;;;;;;;;;;;;;
; These originally called C0/A17F and are being redirected
; to the new location of the subroutine at C0/D68D.
org $C09F6D
JSR $D68D

org $C0A177
JSR $D68D

org $C0C547
JSR $D68D

; When recruiting a character, check if they learn abilities 
; based on their level. If so, teach them all abilities up to 
; their current level.
org $C0D68D
;;;;;;;;;;;;;;;;;;
LDA $1600,Y      ; Grab the ID of the character being recruited.
CMP #$08         ;  
BEQ RelmRecruit  ; If Relm, branch.
CMP #$00         ;
BEQ TerraRecruit ; If Terra, branch.
CMP #$06         ;
BEQ CelesRecruit ; If Celes, branch.
CMP #$02         ;
BEQ CyanRecruit  ; If Cyan, branch.
CMP #$05         ; 
BNE ExitRecruit  ; If not Sabin, exit.
JMP SabinRecruit ; Else it is Sabin, so jump.
                 ; Sabin's recruitment subroutine is to far away
                 ; to branch, so we need to jump to it instead.
ExitRecruit:
RTS
 
RelmRecruit:
LDX $00
TeachRelm:
LDA $ECE3A1,X    ; Get level from natural magic table.
CMP $1608,Y      ; Check against current level.
BEQ RelmRecCont  ; If equal, proceed to learn spell.
BCS ExitRecruit  ; If lower, exit.
RelmRecCont:
PHY	
LDA $ECE3A0,X    ; Grab the spell learned at this level.
TAY
LDA #$FF
STA $1C1E,Y      ; Teach it.
PLY
INX
INX
CPX #$0020       ; Have we checked all 16 spells?
BEQ ExitRecruit  ; If yes, exit.
BRA TeachRelm    ; Else, continue checking.

TerraRecruit:
LDX $00
TeachTerra:
LDA $ECE3C1,X    ; Get level from natural magic table.
CMP $1608,Y      ; Check against current level.
BEQ TerraRecCont ; If equal, proceed to learn spell.
BCS ExitRecruit  ; If lower, exit 
TerraRecCont:
PHY	
LDA $ECE3C0,X    ; Grab the spell learned at this level
TAY
LDA #$FF
STA $1A6E,Y      ; Teach it
PLY
INX
INX
CPX #$0020       ; Have we checked all 16 spells?
BEQ ExitRecruit  ; If so, exit.
BRA TeachTerra   ; Else, continue checking.

CelesRecruit:
LDX $00
TeachCeles:
LDA $ECE3E1,X    ; Get level from natural magic table.
CMP $1608,Y      ; Check against current level.
BEQ CelesRecCont ; If equal, proceed to learn spell.
BCS ExitRecruit  ; If lower, exit 
CelesRecCont:
PHY	
LDA $ECE3E0,X    ; Grab the spell learned at this level
TAY
LDA #$FF
STA $1BB2,Y      ; Teach it
PLY
INX
INX
CPX #$0020       ; Have we checked all 16 spells?
BEQ ExitRecruit  ; If so, exit
BRA TeachCeles   ; Else, continue checking

CyanRecruit:
STZ $1B
LDX $00
TeachCyan:
LDA $E6F490,X    ; Get level at which Tech is learned
CMP $1608,Y      ; Is Cyan at that level?
BEQ CyanRecCont  ; If so, proceed with learning
BCS SetCyan      ; If he is lower, exit and teach Techs.
CyanRecCont:
INC $1B
INX
CPX #$0008       ; Have we checked all eight Techs?
BEQ SetCyan      ; If so, exit
BRA TeachCyan    ; Else, continue checking
SetCyan:
LDA $1B
TAX
LDA $1CF7        ; Get currently known Techs
ORA $C0A22C,X    ; Add to bitfield of Techs learned at level
STA $1CF7        ; Set new Blitzes learned
RTS
 
SabinRecruit:
STZ $1B
LDX $00
TeachSabin:
LDA $E6F498,X    ; Get level at which Blitz is learned
CMP $1608,Y      ; Is Sabin at that level?
BEQ SabinRecCont ; If so, proceed with learning
BCS SetSabin     ; If he is lower, exit and teach Blitzes.
SabinRecCont:
INC $1B
INX
CPX #$0008       ; Have we checked all eight blitzes?
BEQ SetSabin     ; If so, exit
BRA TeachSabin   ; Else, continue checking
SetSabin:
LDA $1B
TAX
LDA $1D28        ; Get currently known Blitzes
ORA $C0A22C,X    ; Add to bitfield of Blitzes learned at level
STA $1D28        ; Set new Blitzes learned
RTS



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; NATURAL MAGIC
; TABLES
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; Relm's natural magic learning list.
; This precedes Celes and Terra's lists in the table,
; occupying free space right before it.
org $ECE3A0
DB $2D, $01 ; Cure @ 1
DB $32, $03 ; Antdot @ 3
DB $02, $08 ; Bolt @ 8
DB $19, $0C ; Slow @ 12
DB $30, $12 ; Life @ 18
DB $07, $16 ; Bolt 2 @ 22
DB $29, $1A ; Osmose @ 26
DB $08, $20 ; Bio @ 32
DB $10, $23 ; Demi @ 35
DB $0B, $28 ; Bolt 3 @ 40
DB $2E, $2E ; Cure 2 @ 46
DB $28, $34 ; Slow 2 @ 52
DB $0E, $3C ; Pearl @ 60
DB $2F, $49 ; Cure 3 @ 73
DB $15, $52 ; Quake @ 82
DB $2B, $61 ; Quick @ 97

; Adjusts Celes's list slightly.
; This fixes a glitch if she averages to a level above 32 that 
; would prevent her learning Muddle and orders the spells more 
; logically.
org $ECE3F0
DW $1E21 ; Muddle @ 23
DW $2128 ; Berserk @ 40